<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Model extends CI_Model {
	
	protected $CI;
	
	function __construct(){
		parent::__construct();
		$this->CI =& get_instance();
	}
	
	function table_name(){
		return get_class($this);
	}
	
	protected function before_save(){
		return true;
	}
	
	function save($attributes=array(),$table=''){
		if ($this->before_save()){
			if ($table == ''){
				$table = $this->table_name();
			}
			if (is_array($attributes) && count($attributes) > 0){
				$column = $this->db->list_fields($table);
				$alias = '';
					if (in_array('created',$column) && !isset($attributes['created'])){
						$attributes['created'] = fdate();
					}
					if (in_array('alias',$column) && ($attributes['alias'] =="" || !isset($attributes['alias']))){
						$alias = $attributes['name'];
					}else if (isset($attributes['alias'])){
						$alias = $attributes['alias'];
					}
					$attributes['alias'] = alias_url($alias);
					if(is_array($column) && count($column) > 0){
						foreach ($attributes as $key=>$value){
							if (!in_array($key,$column)){
								unset($attributes[$key]);
							}
						}
						
						$this->db->insert($table,$attributes);
						$this->after_save();
						if (isset($this->CI->cache)) {
							$this->CI->cache->file->clean();
						}
						return $this->db->insert_id();
				}
			}
		}
		return false;
	}
	
	protected function after_save(){
		
	}
	
	protected function before_update(){
		return true;
	}
	
	function update($attributes = null, $where= NULL, $table=''){
		if ($this->before_update()){
			if ($table == ''){
				$table = $this->table_name();
			}
			if (is_array($attributes)){
				$fileds = $this->db->list_fields($table);
				$alias = '';

				if (in_array('alias', $fileds) && (!isset($attributes['alias']) || $attributes['alias'] =="")){
					$alias = isset($attributes['name']) ? $attributes['name'] : '';
				}else if (isset($attributes['alias'])){
					$alias = $attributes['alias'];
				}
				$attributes['alias'] = alias_url($alias);
				foreach ($attributes as $key=>$value){
					if (!in_array($key,$fileds)){
						unset($attributes[$key]);
					}
				}
				
				$this->db->update($table,$attributes,$where);
				$this->after_update();
				if (isset($this->CI->cache)) {
					$this->CI->cache->file->clean();
				}
				return true;
			}
		}
		return false;
	}
	
	function update_status($status = null, $where= NULL, $table=''){
		if ($this->before_update()){
			if ($table == ''){
				$table = $this->table_name();
			}
			if (isset($status)){
				$this->db->update($table, array('status' => $status), $where);
				$this->after_update();
				if (isset($this->CI->cache)) {
					$this->CI->cache->file->clean();
				}
				return true;
			}
		}
		return false;
	}
	
	protected function after_update(){
		
	}
	
	protected function before_delete(){
		return true;
	}
	
	function delete($where= NULL,$table=''){
		if ($this->before_delete()){
			if (is_string($where)){
				return false;
			}
			if ($table==''){
				$table = $this->table_name();
			}
			if (is_array($where) && count($where) > 0){
				foreach ($where as $key=>$value){
					$this->db->where($key,$value);
				}
			}
			$this->db->delete($table);
			$this->after_delete();
			if (isset($this->CI->cache)) {
				$this->CI->cache->file->clean();
			}
			return true;
		}
		return false;
	}
	
	protected function after_delete(){
		
	}
	
	function load_by_id($primary){
		$this->db->where($this->db->primary(),$primary);
	}
	
}